package pers.w.chaptertalk.funs.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Tab_BookProgress extends Tab_Base {
    static final String NAME = "bookProgress";
    private static final String PROGRESS = "progress";

    static final String CREATE_SQL = "CREATE TABLE IF NOT EXISTS " +
            NAME + " ( _id integer primary key ," +
            CHAPTER_ID + " integer DEFAULT 0," +
            PROGRESS + " integer DEFAULT 0" +
            ") ";

    Tab_BookProgress(SQLiteDatabase db) {
        super(db);
    }

    void add(long bookID) {
       /* ContentValues values = new ContentValues();
        values.put(_ID, bookid);
        db.insert(NAME, null, values);*/
        db.execSQL("insert into " + NAME + " values(" + bookID + ",0,0)");

    }

    public void update(long bookID,long chapterID,long progress){
        ContentValues values = new ContentValues();
        values.put(CHAPTER_ID,chapterID);
        values.put(PROGRESS,progress);
        db.update(NAME,values,_ID +" = "+bookID,null);
    }
    public void update(long bookID,long chapterID){
        update(bookID,chapterID,0);
    }
    /**
     *
     * @param bookID
     * @return 数组【0】返回看到了哪个章节，【1】返回该章节进度
     */
    public BookProgress getProgress(Long bookID){
        BookProgress bookprogress =null;
        Cursor cursor = db.query(NAME,null,_ID +" = "+bookID,null,null,null,null);
        if (cursor.moveToNext()){
            bookprogress = new BookProgress();
            bookprogress.chapterID=cursor.getLong(1);
            bookprogress.progress=cursor.getInt(2);
        }
        cursor.close();
        return bookprogress;
    }

    public static class BookProgress{
        public long chapterID=0,progress=0;
    }
}
